package com.css.dzj.zfjg.modules.seriouslllegal.controller;

import com.alibaba.fastjson.JSON;
import com.css.dzj.zfjg.common.audit.service.SysAuditService;
import com.css.dzj.zfjg.common.util.BeanUtil;
import com.css.dzj.zfjg.common.util.Constant;
import com.css.dzj.zfjg.common.util.R;
import com.css.dzj.zfjg.modules.seriouslllegal.entity.JgLhjgSeriousIllegalEntity;
import com.css.dzj.zfjg.modules.seriouslllegal.service.JgLhjgSeriousIllegalService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;


/**
 * 企业严重违法信息
 *
 * @author zch
 * @email
 * @date 2020-02-17 10:41:37
 */
@RestController
@RequestMapping("generator/jglhjgseriousillegal")
public class JgLhjgSeriousIllegalController {
    @Autowired
    private JgLhjgSeriousIllegalService jgLhjgSeriousIllegalService;

    // base日志接口
    @Autowired
    SysAuditService sysAuditService;
    @Autowired
    HttpServletRequest request;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Integer pageSize, @RequestParam Integer pageNo){
        String s = "";
        try {
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<JgLhjgSeriousIllegalEntity> seriousQueryResults = jgLhjgSeriousIllegalService.queryPage(pageRequest);
            s = JSON.toJSONString(seriousQueryResults);
            sysAuditService.saveSuccessAudit("分页查询企业严重违法信息成功", Constant.ZFJG_JGLHJGSERIOUSILLEGAL, Constant.PT, Constant.ZFJG_PAGE, s, request);
            return R.ok().put("data", seriousQueryResults);
        } catch(Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("分页查询企业严重违法信息失败", Constant.ZFJG_JGLHJGSERIOUSILLEGAL, Constant.YZ, Constant.ZFJG_PAGE, s, e, request);
            return R.error(e.getMessage());
        }
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{seriousIllegalId}")
    public R info(@RequestParam String seriousIllegalId){
        String s = "";
        try {
            JgLhjgSeriousIllegalEntity jgLhjgSeriousIllegal = jgLhjgSeriousIllegalService.findById(seriousIllegalId);
            s = JSON.toJSONString(jgLhjgSeriousIllegal);
            sysAuditService.saveSuccessAudit("通过id获取企业严重违法信息成功", Constant.ZFJG_JGLHJGSERIOUSILLEGAL, Constant.PT, Constant.ZFJG_SELECT, s, request);
            return R.ok().put("jgLhjgSeriousIllegal", jgLhjgSeriousIllegal);
        } catch (Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("通过id获取企业严重违法信息失败", Constant.ZFJG_JGLHJGSERIOUSILLEGAL, Constant.YZ, Constant.ZFJG_SELECT, s, e, request);
            return R.error(e.getMessage());
        }

    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody JgLhjgSeriousIllegalEntity jgLhjgSeriousIllegalEntity, HttpServletRequest request){
        String s  = "";
        try {
            if(StringUtils.isEmpty(jgLhjgSeriousIllegalEntity)) {
                return R.error("企业严重违法信息不能为空");
            }
            jgLhjgSeriousIllegalService.add(jgLhjgSeriousIllegalEntity, request);
            s =JSON.toJSONString(jgLhjgSeriousIllegalEntity);
            sysAuditService.saveSuccessAudit("企业严重违法信息添加成功", Constant.ZFJG_JGLHJGSERIOUSILLEGAL, Constant.PT, Constant.ZFJG_ADD, s, request);
            return R.ok();
        } catch(Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("企业严重违法信息添加失败", Constant.ZFJG_JGLHJGSERIOUSILLEGAL, Constant.YZ, Constant.ZFJG_ADD, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody JgLhjgSeriousIllegalEntity jgLhjgSeriousIllegal){
        String s = "";
        try {
            JgLhjgSeriousIllegalEntity oldJgLhjgSeriousIllegalEntity = jgLhjgSeriousIllegalService.findById(jgLhjgSeriousIllegal.getSeriousIllegalId());
            BeanUtil.copyBeanNotEmptyToBean(jgLhjgSeriousIllegal, oldJgLhjgSeriousIllegalEntity);
            jgLhjgSeriousIllegalService.update(jgLhjgSeriousIllegal);
            s = JSON.toJSONString(jgLhjgSeriousIllegal);
            sysAuditService.saveSuccessAudit("企业严重违法信息修改成功", Constant.ZFJG_JGLHJGSERIOUSILLEGAL, Constant.PT, Constant.ZFJG_UPDATE, s, request);
            return R.ok();
        } catch(Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("企业严重违法信息修改失败", Constant.ZFJG_JGLHJGSERIOUSILLEGAL, Constant.YZ, Constant.ZFJG_UPDATE, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestParam String seriousIllegalId){
        String s = "";
        try {
            JgLhjgSeriousIllegalEntity jgLhjgSeriousIllegalEntity = jgLhjgSeriousIllegalService.findById(seriousIllegalId);
            jgLhjgSeriousIllegalService.delete(jgLhjgSeriousIllegalEntity);
            s = JSON.toJSONString(jgLhjgSeriousIllegalEntity);
            sysAuditService.saveSuccessAudit("企业严重违法信息删除成功", Constant.ZFJG_JGLHJGSERIOUSILLEGAL, Constant.PT, Constant.ZFJG_DELETE, s, request);
            return R.ok();
        } catch(Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("企业严重违法信息删除失败", Constant.ZFJG_JGLHJGSERIOUSILLEGAL, Constant.YZ, Constant.ZFJG_DELETE, s, e, request);
            return R.error(e.getMessage());
        }
    }

}
